<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <div id = "timer"></div>
    <button id="btn">取消定时执行</button>
</body>
<script>
    /**
     * 在页面上生成动态的时钟
     * 2021-10-19 11:48:30
     * 每隔一秒更新
    */
    function identify(num){
       return num < 10?"0"+num:num;
    }
    function showTime(){
        var now = new Date();
        var year = now.getFullYear();
        var month = now.getMonth() + 1;
        var date = now.getDate();
        var hour = now.getHours();
        var minute = now.getMinutes();
        var second = now.getSeconds();
        month = identify(month);
        date = identify(date);
        hour = identify(hour);
        minute = identify(minute);
        second = identify(second);
        return year+"-"+month+"-"+date+"  "+hour+":"+minute+":"+second;
    }
    var timerId = setInterval(function(){
        var result = showTime();
        var timer = document.getElementById("timer");
        timer.innerHTML = result;
    },1000);

    var btn = document.getElementById("btn");
    btn.onclick = function(){
        clearInterval(timerId);
    }
</script>
</html>